<?php
/**
 * V6
 *
 */
class data_follow_circle extends data {
	public function __construct() {
		parent::__construct();
	}
	/**
	 * 是否已经订阅，如果已经订阅，返回该条记录
	 *		
	 * @param int $uid
	 * @param int $circle_id
	 * @return array
	 */
	public function is_followed ($uid, $circle_id) {
		$uid		= intval($uid);
		$circle_id	= intval($circle_id);
		
		if ($uid and $circle_id) {
			$execute = $this->db->execute('
				select 
					*
				from
					follow_circle
				where
					user_id		= "'.$uid.'"				and
					circle_id	= "'.$circle_id.'"
				limit
					1
			');
			if ($execute) {
				return $execute[0];
			} else return false;
		} else return false;
	}
	
	
	/* -------------------------------------------------------------------------------------------------- */
	
	/**
	 * 添加订阅circle，并复制circle中的文章
	 * 		添加订阅源，重复的忽略 Unique()
	 * 		订阅circle的时候判断不是成员
	 * 		复制文章
	 *
	 * @param int $uid
	 * @param int $circle_id
	 * @return int
	 */
	public function add ($uid, $circle_id) {
		$uid		= intval($uid);
		$circle_id	= intval($circle_id);
		
		if ($uid and $circle_id) {
			switch ($source_type) {
				case 'follow_circle':
					$sql = '
						insert ignore into
							follow_circle
						(user_id, circle_id, nickname)
							select
								"'.$uid.'",
								"'.$circle_id.'",
								circle.title
							from
								circle, follow_group
							where
								circle.id				= "'.$circle_id.'"	and
								circle.privacy			= "public"			and
								follow_group.id			= "'.$group_id.'"	and
								follow_group.user_id	= "'.$uid.'"		and
								(select id from circle_member where circle_id="'.$source_id.'" and user_id="'.$uid.'") is null
						';
					break;
				default:
					return false;
			}
			
			$insert_id = $this->db->execute($sql);
			
			if ($insert_id) {
				// 发布event
				$data_event = new data_event();
				$data_event->add('follow_source', $insert_id);
				
				// 更新统计数:组group内follow的总数
				$data_follow_group = new data_follow_group();
				$data_follow_group->count_total($group_id, 1);		// 总数+1

				// 更新源的follower总数
				if ($source_type == 'follow_folder') {
					$data_save_folder = new data_save_folder();
					$data_save_folder->count_follower($source_id, 1);
				} elseif ($source_type == 'follow_rss') {
					$data_rss = new data_rss();
					$data_rss->count_follower($source_id, 1);
				} elseif ($source_type == 'follow_circle') {
					$data_circle = new data_circle();
					$data_circle->count_follower($source_id, 1);
				}
				
				// 复制文章
				$this->copy_article($insert_id, $source_type);

				return $insert_id;
			} else return false;
		} else return false;
	}
	private function copy_article ($follow_source_pk_id, $source_type) {
		$follow_source_pk_id	= intval($follow_source_pk_id);
		if ($follow_source_pk_id) {
			if ($source_type === 'follow_folder') {
				$copy = $this->db->execute('
					insert into
						follow_article
					(user_id, group_id, source_type, source_id, pk_id, url_id, follow_id)
						select
							follow_source.user_id,
							follow_source.group_id,
							follow_source.source_type,
							follow_source.source_id,
							save_article.id,
							save_article.url_id,
							follow_source.id
						from
							follow_source, save_article
						where
							follow_source.id		= "'.$follow_source_pk_id.'"	and
							save_article.folder_id	= follow_source.source_id
						order by
							save_article.id ASC
				');
				$copy2tee = $this->db->execute('
					insert ignore into
						tee
					(user_id, source_type, source_id, pk_id, article_id, url_id, author_id, dt)
						select
							follow_source.user_id,
							follow_source.source_type,
							follow_source.source_id,
							save_article.id,
							save_article.id,
							save_article.url_id,
							save_article.user_id,
							save_article.dt
						from
							follow_source, save_article
						where
							follow_source.id		= "'.$follow_source_pk_id.'"	and
							save_article.folder_id	= follow_source.source_id
						order by
							save_article.id ASC
				');
			} elseif ($source_type === 'follow_rss') {
				$copy = $this->db->execute('
					insert into
						follow_article
					(user_id, group_id, source_type, source_id, pk_id, url_id, follow_id)
						select
							follow_source.user_id,
							follow_source.group_id,
							follow_source.source_type,
							follow_source.source_id,
							rss_article.id,
							rss_article.url_id,
							follow_source.id
						from
							follow_source, rss_article
						where
							follow_source.id		= "'.$follow_source_pk_id.'"			and
							rss_article.rss_id		= follow_source.source_id
				');
				$copy2tee = $this->db->execute('
					insert ignore into
						tee
					(user_id, source_type, source_id, pk_id, article_id, url_id, author_id, dt)
						select
							follow_source.user_id,
							follow_source.source_type,
							follow_source.source_id,
							rss_article.id,
							"0",
							rss_article.url_id,
							"0",
							rss_article.dt
						from
							follow_source, rss_article
						where
							follow_source.id		= "'.$follow_source_pk_id.'"			and
							rss_article.rss_id		= follow_source.source_id
				');
			} elseif ($source_type === 'follow_circle') {
				$copy = $this->db->execute('
					insert into
						follow_article
					(user_id, group_id, source_type, source_id, pk_id, url_id, follow_id)
						select
							follow_source.user_id,
							follow_source.group_id,
							follow_source.source_type,
							follow_source.source_id,
							circle_article.id,
							circle_article.url_id,
							follow_source.id
						from
							follow_source, circle_article
						where
							follow_source.id			= "'.$follow_source_pk_id.'"		and
							circle_article.circle_id	= follow_source.source_id
				');
				$copy2tee = $this->db->execute('
					insert ignore into
						tee
					(user_id, source_type, source_id, pk_id, article_id, url_id, author_id, dt)
						select
							follow_source.user_id,
							follow_source.source_type,
							follow_source.source_id,
							circle_article.id,
							save_article.id,
							save_article.url_id,
							save_article.user_id,
							save_article.dt
						from
							follow_source, circle_article, save_article
						where
							follow_source.id			= "'.$follow_source_pk_id.'"		and
							circle_article.circle_id	= follow_source.source_id			and
							save_article.id				= circle_article.article_id
				');
			} else return false;
			return true;
		} else return false;
	}
	/**
	 * 列出订阅的源
	 * 			如果源是folder，查询出folder的所有者id和nickname
	 *
	 * @param int $uid
	 * @param string $relation					// private / friend / public
	 * @param int $group_id
	 * @param string $source_type				// all / follow_folder / follow_rss / follow_circle
	 * @param int $page
	 * @param int $limit						// 0：没有限制
	 * @return array
	 */
	public function ls ($uid, $relation='public', $group_id=0, $source_type='all/follow_folder/follow_rss/follow_circle', $page=1, $limit=10) {
		$uid		= intval($uid);
		$relation	= ($relation === 'private') ? $relation : 'public';
		$group_id	= intval($group_id);
		$source_type = ($source_type==='all' or $source_type==='follow_folder' or $source_type==='follow_rss' or $source_type==='follow_circle') ? $source_type : 'all';
		$page		= intval($page);
		$page		= ($page>1) ? $page : 1;
		$limit		= intval($limit);
		
		if ($uid) {
			$execute = $this->db->execute('
				select 
					follow_source.*
				from
					follow_source, follow_group
				where
					follow_source.user_id		= "'.$uid.'"													and
					'.(($group_id) ? ' follow_source.group_id = "'.$group_id.'" ' : ' 1 ').' 					and
					'.(($source_type==='all') ? ' 1 ' : ' follow_source.source_type = "'.$source_type.'" ').' 	and
					follow_group.id = follow_source.group_id 													and
					'.(($relation==='private') ? ' 1 ' : ' follow_group.privacy = "public" ').'
				order by
					follow_source.id DESC
				'.(($limit) ? ' limit '.(($page-1)*$limit).', '.$limit.' ' : '').'
			');
			if (is_array($execute)) {
				foreach ($execute as $k=>$v) {
					if ($v['source_type'] == 'follow_folder') {
						$data_save_folder = new data_save_folder();
						$folder = $data_save_folder->one($uid, $v['source_id']);
						if ($folder) {									// 如果源是folder，查询出folder的所有者id和nickname
							$data_user = new data_user();
							$execute[$k]['source_owner_id'] 		= $folder['user_id'];
							$execute[$k]['source_owner_nickname'] 	= $data_user->friend_to_nickname($uid, $folder['user_id']);
						}
					} elseif ($v['source_type'] == 'follow_rss') {
						$data_rss = new data_rss();
						$rss = $data_rss->one($v['source_id']);
						if ($rss) {
							$execute[$k]['source_link'] = $rss['link'];
						}
					}
				}
				return $execute;
			} else return false;
		} else return false;
	}
	/**
	 * 获取一条订阅源的记录
	 * 		判断权限
	 *
	 * @param int $uid
	 * @param int $id						// 主键id
	 * @return array
	 */
	public function one ($uid, $id) {
		$uid	= intval($uid);
		$id		= intval($id);
		
		if ($uid and $id) {
			$execute = $this->db->execute('
				select
					follow_source.*,
					follow_group.title			AS group_title,
					follow_group.privacy		AS group_privacy
				from
					follow_source, follow_group
				where
					follow_source.id		= "'.$id.'"					and
					follow_group.id			= follow_source.group_id	and
					(follow_group.privacy = "public" or follow_group.user_id = "'.$uid.'")
			');
			if ($execute) {
				return $execute[0];
			} else return false;
		} else return false;
	}
	/**
	 * 修改源的昵称和分组
	 * 			当分组改变时，重新统计组内文章总数
	 *
	 * @param int $uid
	 * @param int $id
	 * @param string $nickname				// POST Magic Quote, encodeURIComponent
	 * @param int $group_id
	 * @return bool
	 */
	public function edit ($uid, $id, $nickname, $group_id) {
		$uid		= intval($uid);
		$id			= intval($id);
		$nickname	= strip_tags(trim($nickname));
		$group_id	= intval($group_id);
		
		if ($uid and $id and $nickname and $group_id) {
			$execute = $this->db->execute('
				select 
					* 
				from 
					follow_source 
				where 
					id		= "'.$id.'" 	and 
					user_id	= "'.$uid.'"
			');
			if ($execute) {
				$update = $this->db->execute('
					update
						follow_source
					set
						group_id		= "'.$group_id.'",
						nickname		= "'.$nickname.'"
					where
						id		= "'.$id.'"
				');
				if ($execute[0]['group_id'] <> $group_id) {
					// 更新follow_article中的组记录
					$this->db->execute('
						update
							follow_article
						set
							group_id	= "'.$group_id.'"
						where
							group_id	= "'.$execute[0]['group_id'].'"
					');
					// 更新组中的统计数
					$data_follow_group = new data_follow_group();
					$data_follow_group->count_total($execute[0]['group_id'], -1);
					$data_follow_group->count_total($group_id, 1);
				}
				return $update;
			} else return false;
		} else return false;
	}
	/**
	 * 删除订阅源，并删除订阅的文章
	 *
	 * @param int $uid
	 * @param int $pkid
	 * @return bool
	 */
	public function del ($uid, $pkid) {
		$uid		= intval($uid);
		$pkid		= intval($pkid);
		
		if ($uid and $pkid) {
			// 检查删除的项目是不是合法
			$one = $this->db->execute('
				select 
					* 
				from 
					follow_source 
				where 
					id			= "'.$pkid.'" 	and 
					user_id		= "'.$uid.'"
			');
			if ($one) {
				// 删除订阅文章
				$del_article = $this->db->execute('
					delete from
						follow_article
					where
						follow_id	= "'.$pkid.'"
				');
				// 删除订阅源
				$del = $this->db->execute('
					delete from 
						follow_source
					where
						id			= "'.$pkid.'"
				');
				// 删除tee中的文章
				$del_f = $this->db->execute('
					delete from
						tee
					where
						user_id		= "'.$one[0]['user_id'].'"		and
						source_type	= "'.$one[0]['source_type'].'"	and
						source_id	= "'.$one[0]['source_id'].'"
				');
				// 统计group中的总数
				$data_follow_group = new data_follow_group();
				$data_follow_group->count_total($one[0]['group_id'], -1);			// 总数-1
				
				// 更新源的follower总数
				if ($one[0]['source_type'] == 'follow_folder') {
					$data_save_folder = new data_save_folder();
					$data_save_folder->count_follower($one[0]['source_id'], -1);
				} elseif ($one[0]['source_type'] == 'follow_rss') {
					$data_rss = new data_rss();
					$data_rss->count_follower($one[0]['source_id'], -1);
				} elseif ($one[0]['source_type'] == 'follow_circle') {
					$data_circle = new data_circle();
					$data_circle->count_follower($one[0]['source_id'], -1);
				}
				
				return $del;
			} else return false;
		} else return false;
	}
}
?>